/* $RCSFile: ADNDynInvoiceDetail.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/CRMWrapper3/DynEntities/ADNDynInvoiceDetail.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:33  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

using CrmSdk;

namespace AddonNice.CRMWrapper
{
	public class ADNDynInvoiceDetail : ADNDynSalesDetailBase
	{
        protected class Ftor: DFactor
        {
            public Ftor() : base(CrmStr.invoicedetail)
            {}

            public override Type DynType
            { get { return typeof(ADNDynInvoiceDetail); } }

            public override string DynTypename
            {   get{ return CrmStr.invoicedetail;}}

            public override DynEntity Build()
            {   return new ADNDynInvoiceDetail();}

            public override DynEntity Build(CRMWrapperBase w, InternalDynamicEntity dEntity, PropertyDescriptorCollection wproperties)
            {   return new ADNDynInvoiceDetail(w, dEntity, wproperties, null);}

            public override DynEntity Build(CRMWrapperBase w, string PrimaryFieldValue)
            {   return new ADNDynInvoiceDetail(w);}

            public override DynEntity Build(CRMWrapperBase w,Guid PrimaryKeyValue)
            {   return new ADNDynInvoiceDetail(w,PrimaryKeyValue);}
        }

        static ADNDynInvoiceDetail(){new Ftor();} 

        /// <summary>
		/// Reserved for template methods: do not use directly
		/// </summary>
        public ADNDynInvoiceDetail(): base()
		{}

		public ADNDynInvoiceDetail(CRMWrapperBase w)
			: base(w,CrmStr.invoicedetail)
		{}

		public ADNDynInvoiceDetail(ResetWrapperHandler fn)
			: base(fn,CrmStr.invoicedetail)
		{}

		public ADNDynInvoiceDetail(CRMWrapperBase w, Guid CrmId)
			: base(w,CrmStr.invoicedetail, CrmId)
		{}

		public ADNDynInvoiceDetail(ResetWrapperHandler fn, Guid CrmId)
			: base(fn,CrmStr.invoicedetail, CrmId)
		{}

        public ADNDynInvoiceDetail(CRMWrapperBase w, InternalDynamicEntity e)
			: base(w,CrmStr.invoicedetail,e,null,null)
		{}

        public ADNDynInvoiceDetail(ResetWrapperHandler fn, InternalDynamicEntity e)
			: base(fn,CrmStr.invoicedetail,e,null,null)
		{}

		public ADNDynInvoiceDetail(CRMWrapperBase w, InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s)
			: base(w,CrmStr.invoicedetail,e,prop,s)
		{}

		public ADNDynInvoiceDetail(ResetWrapperHandler fn, InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s)
			: base(fn,CrmStr.invoicedetail,e,prop,s)
		{}

        /// <summary>
		/// Crm Key
		/// </summary>
        [CrmDynAttribute(CrmStr.invoicedetail)]
        public Guid invoicedetailid
		{
			get
			{
				return KeyPropertyValue(CrmStr.invoicedetailid);
			}
			set
			{
				AddKeyProperty(CrmStr.invoicedetailid,value);
			}
		}

        protected override void SetCrmIdProperty(Guid crmId)
        {
           if ( !dicProperty.ContainsKey(CrmStr.invoicedetailid) || KeyPropertyValue(CrmStr.invoicedetailid) != crmId )
                invoicedetailid =   crmId;
        }
 
 
        public override Guid PrimaryKeyGuidValue
        {
           get
           {
                return invoicedetailid;
           }
           set
           {
               invoicedetailid    =   value;
           }
        }

        /// <summary>
        /// Gets or sets the value that indicates if the line was copied from a quote. 
        /// </summary>
        [CrmDynAttribute(CrmStr.invoicedetail)]
        public bool iscopied
        {
            get 
            {
                return BooleanPropertyValue(CrmStr.iscopied);
            }
            set
            {
            
                AddBooleanProperty(CrmStr.iscopied,value);
            }
        }

        [CrmDynAttribute(CrmStr.invoicedetail)]
        public Guid invoiceid
		{
			get
			{
				return LookupPropertyValue(CrmStr.invoiceid);
			}
			set
			{
				AddLookupProperty(CrmStr.invoiceid,CrmStr.invoice, value);
			}
		}
 
        /// <summary>
        /// Gets or sets the quantity that has been backordered for the sales order detail (order product). 
        /// </summary>
        [CrmDynAttribute(CrmStr.invoicedetail)]
        public decimal quantitybackordered
        {
            get
            {
                return MoneyPropertyValue(CrmStr.quantitybackordered);
            }
            set
            {
                AddMoneyProperty(CrmStr.quantitybackordered,value);
            }
        }
 
        /// <summary>
        /// Gets or sets the quantity that was canceled for the sales order detail (order product). 
        /// </summary>
        [CrmDynAttribute(CrmStr.invoicedetail)]
        public decimal quantitycancelled
        {
            get
            {
                return MoneyPropertyValue(CrmStr.quantitycancelled);
            }
            set
            {
                AddMoneyProperty(CrmStr.quantitycancelled,value);
            }
        }

        /// <summary>
        /// Gets or sets the quantity shipped for the product on the order. 
        /// </summary>
        [CrmDynAttribute(CrmStr.invoicedetail)]
        public decimal quantityshipped
        {
            get
            {
                return MoneyPropertyValue(CrmStr.quantityshipped);
            }
            set
            {
                AddMoneyProperty(CrmStr.quantityshipped,value);
            }
        }
 
        /// <summary>
        /// Gets whether the sales order detail (order product) pricing is locked.  
        /// </summary>
        [CrmDynAttribute(CrmStr.invoicedetail)]
        public bool invoiceispricelocked
        {
            get 
            {
                return BooleanPropertyValue(CrmStr.invoiceispricelocked);
            }
            set
            {
            
                AddBooleanProperty(CrmStr.invoiceispricelocked,value);
            }
        }

        /// <summary>
        /// Gets or sets the date on which the product on the invoice is delivered.
        /// </summary>
        [CrmDynAttribute(CrmStr.invoicedetail)]
        public string actualdeliveryon
        {
            get
            {
                return DateTimePropertyValue(CrmStr.actualdeliveryon);
            }
            set
            {
                AddDateTimeProperty(CrmStr.actualdeliveryon,value);
            }
        }
        
        /// <summary>
        /// Gets or sets the tracking number for shipping the invoice product line item. 
        /// </summary>
        [CrmDynAttribute(CrmStr.invoicedetail)]
        public string shippingtrackingnumber
        {
            get
            {
                return StringPropertyValue(CrmStr.shippingtrackingnumber);
            }
            set
            {
                AddStringProperty(CrmStr.shippingtrackingnumber,value);
            }
        }
        
        /// <summary>
        /// Gets the status of the invoice detail (order product).
        /// </summary>
        [CrmDynAttribute(CrmStr.invoicedetail)]
        public int invoicestatecode
        {
            get 
            {
                return PicklistPropertyIntValue(CrmStr.invoicestatecode);
            }
        }

        /// <summary>
        /// find  by selecting same order same product same quantity and same amount
        /// </summary>
        public bool FindByInvoiceProduct(ResetWrapperHandler fn,Guid ivID,Guid prodID,decimal quantity,decimal price)
        {
            if ( CRMDynTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ADNDynInvoiceDetail FindByInvoiceProduct BEG ivID: {0}, prodID: {1}, quantity: {2}, price: {3}",
                    new object[]{ivID,prodID,quantity,price}),CRMDynTraceSwitch.Sw.Info);
            try
            {
                GenericDynQueryMultiple<ADNDynInvoiceDetail> allEntQuery      =   new GenericDynQueryMultiple<ADNDynInvoiceDetail>(wrapper,fn,CrmStr.invoicedetail);
                InternalQueryExpression qe                          =   new InternalQueryExpression();
                qe.EntityName                                       =   CrmStr.invoicedetail;
                InternalAllColumns RequestCols                      =   new InternalAllColumns();
                InternalFilterExpression fi                         =   new InternalFilterExpression();
                InternalConditionExpression ex                      =   new InternalConditionExpression();
                ex.AttributeName                                    =   CrmStr.invoiceid;
                ex.Operator                                         =   InternalConditionOperator.Equal;
                ex.Values                                           =   new string[]{ivID.ToString()};
                InternalConditionExpression ex1                     =   new InternalConditionExpression();
                ex1.AttributeName                                   =   CrmStr.productid;
                ex1.Operator                                        =   InternalConditionOperator.Equal;
                ex1.Values                                          =   new string[]{prodID.ToString()};
                fi.Conditions                                       =   new InternalConditionExpression[]{ex,ex1};
                qe.ColumnSet                                        =   RequestCols;
                qe.Criteria                                         =   fi;
                InternalBusinessEntityCollection allEnt             =   allEntQuery.adminRetrieve(qe);
                ADNDynInvoiceDetail ob                              =   null;
                bool found                                          =   false;
                if ( allEntQuery.RetrievedCount > 0 )
                {
                    wrapper.Util.ExecForEachDynamicEntity(allEnt,delegate(InternalDynamicEntity de)
                        {
                            ob               =   new ADNDynInvoiceDetail(wrapper,de,null,null);
                            if ( ob.quantity == quantity && ob.priceperunit == price )
                            {
                                wrapper.FillDicProperties(de,null,dicProperty);
                                found       =   true;
                                if ( CRMDynTraceSwitch.Sw.TraceVerbose )
                                    Trace.WriteLine( "[V]ADNDynInvoiceDetail FindByInvoiceProduct found.",CRMDynTraceSwitch.Sw.Info);
                                return false; //break
                            }
                            return true;// continue
                        });
                }
                return found;
            }
            catch(Exception ex)
            {
                if ( CRMDynTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]ADNDynInvoiceDetail FindByInvoiceProduct ivID: {0}, prodID: {1}, ex: {2}",
                        ivID,prodID,ex),CRMDynTraceSwitch.Sw.Info);
                throw;
            }
        }
    }
}
